package com.kaiwukj.android.ufamily.mvp.presenter;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.di.scope.ActivityScope;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.contract.RecycleVideoContract;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.CommentParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommentResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

/**
 * @author Charles
 * @date 2019/8/14.
 * description：Kaiwu
 */

@ActivityScope
public class RecycleVideoPresenter extends BasePresenter<RecycleVideoContract.Model, RecycleVideoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public RecycleVideoPresenter(RecycleVideoContract.Model model, RecycleVideoContract.View rootView) {
        super(model, rootView);
    }

    /**
     * icon_social_post_card
     */
    public void postLike(int noteId) {
        mModel.postLike(noteId)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<Integer>(mErrorHandler) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.LikeSuccess();
                    }

                });
    }

    /**
     * 取消点赞
     */
    public void postLikeCancel(int noteId) {
        mModel.postLikeCancel(noteId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.LikeCancelSuccess();
                    }

                });
    }

    /**
     * 获取帖子的评论列表
     *
     * @param id 帖子ID
     */
    public void requestCommentList(int id, int startId) {
        mModel.requestCommentList(id, startId)
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                })
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<ListResp<CommentResult>>(mErrorHandler) {
                    @Override
                    public void onSuccess(@NonNull ListResp<CommentResult> result) {
                        mRootView.getCommentList(result.getList());
                    }
                });
    }

    /**
     * 发表或者回复别人的帖子
     */
    public void requestCommentOther(CommentParams request) {
        mModel.requestCommentOther(request)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<Integer>(mErrorHandler) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("评论成功");
                        mRootView.commentSuccess();
                    }
                });
    }

}
